﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using SimpleTiles.Classes;
using System.Diagnostics;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Scheduler;


namespace SimpleTiles
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();

            //da fare all'uscita
           /* var mainTile = ShellTile.ActiveTiles.First();
            mainTile.Update(new StandardTileData
            {
                Count = ShellTile.ActiveTiles.Count()
            });
            */

            //testNotesManager();
            // Console.WriteLine("geppo puzza");


            this.NoteListBox.DataContext = App.nm.Notes;
            
        }

       /* public void testNotesManager()
        {
            // Cancella file note
            NoteManager noteManager = App.nm;
            noteManager.clearNotes();

            noteManager.loadNotes();

            // Aggiungo le note
            noteManager.addNote(new Note("titolo", "testo", DateTime.Now, "url1234", true));
            noteManager.addNote(new Note("titolo1", "testo1", DateTime.Now, "url123", false));
            noteManager.addNote(new Note("titolo2", "testo2", DateTime.Now, "url12", true));
            noteManager.addNote(new Note("questa è una nota dal titolo molto molto lungoooooooo lol", "testo3", DateTime.Now, "url1", false));

            // Salvo la lista delle note creata
            noteManager.saveNotes();
        }*/

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/NotePage.xaml", UriKind.Relative));
        }

        private void Pin_Note(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            Note note = b.DataContext as Note;
            if (!note.Pin)
            {
                //MessageBox.Show("PIN " + note.Titolo);
               // NoteListBox.DataContext = null;
                //NoteListBox.DataContext = App.nm.Notes;
                note.Update();
                TileManager.createTile(note);
            }
            else
            {
                TileManager.DeleteTile(note);
                note.Update();
                //MessageBox.Show("UNPIN " + note.Titolo);
                //NoteListBox.DataContext = null;
                //NoteListBox.DataContext = App.nm.Notes;
            }
        }

        private void Delete_Note(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            Note note = b.DataContext as Note;

            MessageBoxResult result = MessageBox.Show("Sei sicuro di voler cancellare la nota dal titolo " + note.Titolo + "?", "Info", MessageBoxButton.OKCancel);
            if (result == MessageBoxResult.OK)
            {

                if (note.Pin)
                {
                    TileManager.DeleteTile(note);
                }

                App.nm.removeNote(note);
                //TODO: Bisogna cancellare anche l'immagine lo potremmo fare dentro a removeNote tanto ce' l'uri
                App.nm.saveNotes();
                IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();

                var filename = "/Shared/ShellContent/note" + note.Titolo + ".jpg";
                var imgname = "/Shared/ShellContent/" + note.Titolo + ".jpg";
                if (storage.FileExists(filename)) 
                { 
                storage.DeleteFile(filename);
                
                }
                if (storage.FileExists(imgname))
                {
                    storage.DeleteFile(imgname);
                    
                }
                if (ScheduledActionService.Find(note.Titolo) != null)
                {
                    ScheduledActionService.Remove(note.Titolo);
                }
            }
        }

        private void NoteListBox_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
        	// TODO: Add event handler implementation here.
			Note selected;
            if (sender != null)
            {
                // Naviga solo se tappo sull'immagine o sulla textblock e non sui bottoni.
                System.Windows.Controls.Image element1 = e.OriginalSource as System.Windows.Controls.Image;
                System.Windows.Controls.TextBlock element2 = e.OriginalSource as System.Windows.Controls.TextBlock;
                System.Windows.Controls.Border element3 = e.OriginalSource as System.Windows.Controls.Border;

                // Nel caso dei bottoni il Border ha nome BackgroundButton
                if (element1 != null || element2 != null || (element3!=null && element3.Name.Equals("LayoutRoot")))
                {
                    selected = ((ListBox)sender).SelectedItem as Note;
                    if (selected != null)
                    {
                        NavigationService.Navigate(new Uri("/NotePage.xaml?titolo=" + selected.Titolo, UriKind.Relative));
                    }
                }
            }
        }
    }
}